package cn.tedu.charging.order.rabbit;

import cn.tedu.charging.order.config.RabbitConfiguration;
import cn.tedu.charging.order.constant.OrderStatusConstant;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Order;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RabbitMQOrderConsumer {

//    @Autowired
//    private OrderSuccessMapper orderSuccessMapper;
    @Autowired
    private OrderRepository orderRepository;

    /**
     * 消费死信队列的消息
     * 判断订单数据 如果状态是充电中 异常订单 把订单的状态修改为异常结束
     * 如果状态是充电完成, 正常订单,无需处理
     * @param orderMQPO
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitConfiguration.DEAD_LETTER_QUEUE_NAME)
    public void consumeChargingQueue (OrderMQPO orderMQPO, Message message, Channel channel) throws IOException {
        log.debug("消费死信队列消息:{}",orderMQPO);
        //把状态是 正在充电中的 成功订单数据的状态 修改为 异常结束
        //update charging_bill_success
        //set bill_status = 3(异常结束)
        //where bill_id = orderNo and bill_status = 1 (充电中)
        //2 two to
        //方法定义更加明确
        //orderSuccessMapper.updateOrderStatusFromProcess2ExceptionEnd(order);
        orderRepository.updateOrderStatusFromProcess2ExceptionEnd(orderMQPO.getOrderNo());

        //手动ack 可以手动把下面的两行代码注释掉，会发现，消息队列中的消息不会被删除，会一直保留
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,true);
    }
}
